/**
 * 缓存配置
 */
package com.ssl.spring.security.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import com.ssl.spring.security.cache.CacheConstant;
import com.ssl.spring.security.cache.ICache;
import com.ssl.spring.security.cache.ehcache.EhcacheCache;
import com.ssl.spring.security.cache.redis.RedisCache;

import net.sf.ehcache.CacheManager;

/**
 * @author
 * @date 2021年4月29日 上午10:41:10
 * @version 1.0
 */
@Configuration
@EnableCaching
public class CacheConfig {

	@Autowired(required = false)
    private RedisTemplate redisTemplate;
    @Autowired(required = false)
    private CacheManager cacheManager;
    
    @Bean(name = "cache")
    @ConditionalOnProperty(prefix = "spring.cache", name = "type", havingValue = "redis")
    public ICache redis() {
        return new RedisCache(CacheConstant.PREFIX_API, CacheConstant.SUFFIX, redisTemplate);
    }

    @Bean(name = "cache")
    @ConditionalOnProperty(prefix = "spring.cache", name = "type", havingValue = "ehcache")
    public ICache ehcache() {
        System.setProperty(net.sf.ehcache.CacheManager.ENABLE_SHUTDOWN_HOOK_PROPERTY, "true");
        return new EhcacheCache(CacheConstant.PREFIX_API, CacheConstant.SUFFIX, cacheManager);
    }
}
